package cn.edu.cug.cs.gtl.plugins.catalog.tree;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.actions.AbstractAction;
import cn.edu.cug.cs.gtl.apis.ui.datake.DatakeApp;
import cn.edu.cug.cs.gtl.apis.ui.datake.DatakeFrame;
import cn.edu.cug.cs.gtl.apis.ui.datake.catalogtree.CatalogTree;
import cn.edu.cug.cs.gtl.apis.ui.tree.TreeNodeData;
import cn.edu.cug.cs.gtl.protos.DataCatalog;
import cn.edu.cug.cs.gtl.protos.DataGroup;
import cn.edu.cug.cs.gtl.utils.postgresql.DataCatalogManager4Postgresql;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.ActionEvent;
import java.util.Locale;
import java.util.ResourceBundle;

public class AddChildGroupAction extends AbstractAction {
    @Override
    public void initialize(AbstractApplication theApp) {
        this.theApp = theApp;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("catalog-resource", Locale.getDefault());

        name = resourceBundle.getString("popupmenu.catalog.tree.add_child_group");
        menuItem.setText(name);
        menuItem.addActionListener(this);
        ((CatalogTree) (getDatakeFrame().getCatalogPanel().getTreeComponent())).getCatalogNodePopupMenu().add(menuItem);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        CatalogTree tree = (CatalogTree) getDatakeFrame().getCatalogPanel().getTreeComponent();
        DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

        StringBuilder ChildNodeName = new StringBuilder();
        StringBuilder ChildNodeAlias = new StringBuilder();
        StringBuilder ChildNodeDescription = new StringBuilder();
        boolean ret = InputDialog.showInputGroupDialog(ChildNodeName, ChildNodeAlias, ChildNodeDescription);
        if (!ret)
            return ;
        DataGroup childGroup = DataGroup.newBuilder().setName(ChildNodeName.toString()).setAlias(ChildNodeAlias.toString())
                .setDescription(ChildNodeDescription.toString()).build();

        //在数据库中插入一条记录
        DataCatalog parentCatalog = (DataCatalog) ((TreeNodeData) (selectNode.getUserObject())).getRefObject();
        ret = new DataCatalogManager4Postgresql().addDataGroupToCatalog(parentCatalog.getName(), childGroup);
        if (!ret) {
            JOptionPane.showMessageDialog(null, "插入失败，可能该名称已存在", "错误", JOptionPane.ERROR_MESSAGE);
        }

        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new TreeNodeData(childGroup, childGroup.getAlias(), true), true);
        newNode.setAllowsChildren(true);

        DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel();
        treeModel.insertNodeInto(newNode, selectNode, selectNode.getChildCount());

        TreeNode[] nodes = treeModel.getPathToRoot(newNode);
        tree.scrollPathToVisible(new TreePath(nodes));
    }

    public DatakeFrame getDatakeFrame() {
        return (DatakeFrame) (((DatakeApp)theApp).getMainFrame());
    }
}
